import Dialog from "../../../component/vant/dialog/dialog";

let app = getApp()
const util = require('../../../utils/util.js')

Page({
    /**
     * 页面的初始数据
     */
    data: {
        spinShow: false,
        loadMoreLoad: false,
        loadMoreTip: '',
        queryParam: {
            pageNum: 1,
            pageSize: app.globalData.pageSize,
            customerId: null
        },
        //拿货记录数据
        tableData: [],
        //客户详情
        customer: {},
        pages: 0,
        //搜索字段
        searchVal: "",

        //action-sheet
        actions: [
            {name: '新增', type: "add"},
            {name: '工作台', type: "homepage"},
        ],
        actionSheetShow: false,
    },

    showPopup() {
    },


    onPullDownRefresh() {
        this.setData({
            spinShow: true
        });
        if (!this.loading) {
            this.setData({
                ['queryParam.pageNum']: 1,
                loadMoreLoad: false,
                loadMoreTip: '正在刷新',
            });
            this.queryData(true)
        }
    },
    onReachBottom() {
        if (!this.loading && this.data.queryParam.pageNum < this.data.pages) {
            this.setData({
                loadMoreLoad: true,
                loadMoreTip: '正在加载'
            });
            this.setData({
                ['queryParam.pageNum']: this.data.queryParam.pageNum + 1
            });
            this.queryData(false)
        }
    },
    queryData: function (override) {
        let _this = this
        let data = this.data.queryParam
        // data.searchVal = this.data.searchVal
        // data.status = this.data.status
        app.formGet('/app/order/getOrderPage', data).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                let tableData = override ? res.data.list : this.data.tableData.concat(res.data.list)
                _this.setData({
                    tableData: tableData,
                    pages: res.data.pageInfo.pages
                });
                if (this.data.queryParam.pageNum >= res.data.pageInfo.pages) {
                    this.setData({
                        loadMoreLoad: false,
                        loadMoreTip: '暂无更多数据'
                    });
                }
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })

        app.formGet('/app/customer/info?id=' + this.data.queryParam.customerId, data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                _this.setData({
                    customer: res.data
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let customerId = options.customerId
        this.setData({
            spinShow: true,
            queryParam: {
                pageNum: 1,
                pageSize: app.globalData.pageSize,
                customerId: customerId
            },
            loadMoreLoad: false,
            loadMoreTip: ''
        });
    },
    onShow: function () {
        this.queryData(true)
    },

    moreFun: function () {
        this.setData({
            actionSheetShow: true
        })
    },
    actionSelect: function (e) {
        let type = e.detail.type
        if (type == 'add') {
            wx.redirectTo({
                url: "/pages/customer/edit/customerEdit"
            })
        } else if (type == 'homepage') {
            wx.switchTab({
                url: "/pages/index/index"
            })
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    btnDel: function () {
        Dialog.confirm({
            title: '温馨提示',
            message: '确定要删除吗?',
        })
            .then(() => {
                console.log("confirm")
                let data = {}
                data.id = this.data.queryParam.customerId
                app.formPost('/app/customer/del', data).then(res => {
                    wx.hideLoading()
                    if (res.code === 0) {
                        setTimeout(function () {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 2000)
                    } else {
                        app.toast(res.message, "fail")
                    }
                }).catch(e => {
                    _this.setData({
                        spinShow: false
                    });
                    app.message(e, 'error')
                })
            })
            .catch(() => {
                console.log("concel")
                // on cancel
            });
    },
    btnEdit: function () {
        wx.navigateTo({
            url: "/pages/customer/edit/customerEdit?customerId=" + this.data.queryParam.customerId
        })
    }
})
